import React, { Component } from 'react';
import { Form, Row, Col } from 'antd';
import ShowType from '../../../routes/common/showType';
import ReserveFields from '../../Common/ReserveFields/ReserveFields';
import styles from '../../../index.less';
import * as config from '../../../utils/config';

const FormItem = Form.Item;

class TabMainTableComponent extends Component {
  constructor(props) {
    super(props);
    this.state = {
      sTabId: props.app.currentPane.key, /* 当前标签 */
      componentName: props.componentName, /* 组件名 */
      masterConfig: props.masterConfig, /* 配置 */
      sId: props.sId, /* 业务数据Id */
      dataList: [], /* 数据 */
      sActiveId: props.sActiveId, /* 窗体Id */
      disabled: props.disabled, /* 是否是查看状态 */
      tCreateDate: '', /* 单据日期 */
      sMakePerson: '', /* 单据|制单 */
      sBillNo: '', /* 单据编号 */
      sMemo: '', /* 备注 */
    };
  }

  componentWillMount() {
    /* 获取主表数据 */
    const {
      masterConfig, sActiveId, sId, componentName, sTabId,
    } = this.state;
    const { dispatch } = this.props;
    if (sId !== undefined && sId !== '') {
      const mainId = masterConfig.sId;
      /* 表Id */
      const value = { sId, pageSize: '', pageNum: '' };
      /* 业务数据Id */
      const dataUrl = `${config.server_host}business/getBusinessDataByFormcustomId/${mainId}?sModelsId=${sActiveId}`;
      dispatch({
        type: 'content/getUrlPostData',
        payload: {
          url: dataUrl, sTabId, componentName, value, formRoute: '/salsalesordermaster', returnType: 'data',
        },
      });
      // this.props.onFormData('CustomTable', dataUrl, value, []);
      /* 获取主表数据 */
    }
    if (masterConfig.gdsconfigformslave !== undefined) {
      const mainConfig = masterConfig.gdsconfigformslave;
      this.setState({
        sId: sId !== undefined ? sId : '',
        tCreateDate: mainConfig.filter(item => (item.sName.toLowerCase() === 'tcreatedate'))[0],
        sMakePerson: mainConfig.filter(item => (item.sName.toLowerCase() === 'smakeperson'))[0],
        sBillNo: mainConfig.filter(item => (item.sName.toLowerCase() === 'sbillno'))[0],
        sMemo: mainConfig.filter(item => (item.sName.toLowerCase() === 'smemo'))[0],
      });
    }
  }

  componentWillReceiveProps(nextProps) {
    const { sId, disabled, content } = nextProps;
    const { dataList } = content;
    const {
      sDataListTabId, returnType, urlData, loading, componentName,
    } = dataList;
    if (this.state.sTabId === sDataListTabId && this.state.sTabId === nextProps.app.currentPane.key
      && this.state.componentName === componentName) {
      if (loading) return;
      if (returnType === 'data') {
        this.setState({
          dataList: urlData,
          sId: sId !== undefined ? sId : '',
        });
      }
    }
    this.setState({ disabled });
  }

  shouldComponentUpdate(nextProps, nextState) {
    const { componentName } = nextProps;
    const { sId } = this.props;
    if (sId === undefined) {
      return true;
    }
    let flag = true;
    if (nextState.disabled !== this.state.disabled) {
      return true;
    }
    if (this.state.sTabId === nextProps.app.currentPane.key &&
      this.state.componentName === componentName) {
      flag = true;
    } else {
      flag = false;
    }
    return flag;
  }

  render() {
    const {
      app, form, handleModalPane, dataName, dataType, onClose,
    } = this.props;
    const {
      tCreateDate, sMakePerson, sBillNo, sMemo,
    } = this.state;
    const {
      dataList, sId, sActiveId, disabled, masterConfig,
    } = this.state;
    const childProps = {
      app,
      form,
      sActiveId,
      dataList: dataList[0],
      sId,
      disabled,
      handleModalPane,
      dataName,
      dataType,
      onClose,
    };
    const mainConfig = masterConfig.gdsconfigformslave;
    const tCreateDateProps = { showConfig: tCreateDate, ...childProps };
    const sMakePersonProps = { showConfig: sMakePerson, ...childProps };
    const sBillNoProps = { showConfig: sBillNo, ...childProps };
    const sMemoProps = { showConfig: sMemo, ...childProps };
    const mainConfigProps = { mainConfig, ...childProps };
    return (
      <FormItem className={styles.searchForm}>
        <Row type="flex" justify="space-between">
          <Col span={7}>
            <ShowType required sName="tCreateDate" showDefaultName="单据日期" {...tCreateDateProps} />
            <ShowType required sName="sMakePerson" showDefaultName="单据|制单" {...sMakePersonProps} />
            <ShowType sName="sBillNo" showDefaultName="单据编号" {...sBillNoProps} />
          </Col>
          <Col span={7}>
            <ShowType sName="sMemo" textArea rows={5} showDefaultName="备注" {...sMemoProps} />
          </Col>
          <Col span={7}>
            <ReserveFields {...mainConfigProps} />
          </Col>
        </Row>
      </FormItem>
    );
  }
}

export default TabMainTableComponent;
